package com.hennissy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hennissy.entity.ServiceScope;
import com.hennissy.entity.Store;
import com.hennissy.service.ServiceScopeService;
import com.hennissy.service.StoreService;
import com.hennissy.utils.GetStoreId;
import com.hennissy.utils.RedisUtil;
import com.hennissy.utils.ResultAjax;
import com.hennissy.vo.StoreVo;
import com.hennissy.vo.ProvincesVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjh
 * @since 2021-06-19
 */
@CrossOrigin("*")
@Slf4j
@RestController
@RequestMapping("/store_api")
public class StoreController {

    @Autowired
    private StoreService storeService;
    @Autowired
    private ServiceScopeService serviceScopeService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("查看门店信息接口")
    @GetMapping("/getById")
    public ResultAjax getById(@RequestParam(value = "id",required = false) @ApiParam(value = "门店编号") Integer id){

        log.info("id:"+id);
        if (!Objects.isNull(id)){
            Store redisStore = (Store) redisUtil.get("store:" + id);
            if (!Objects.isNull(redisStore)){
                log.info("===从redis获取===");
                return ResultAjax.ok(200,"ok",redisStore);
            }else {
                StoreVo storeVo = new StoreVo();
                storeVo.setId(id);
                List<Store> stores = storeService.storeList(storeVo);
                if (stores.size()==1){
                    redisUtil.set("store:"+id,stores.get(0),60*60*24*7);//604800s七天时间
                    log.info("===保存redis===");
                    return ResultAjax.ok(200,"ok",stores.get(0));
                }else {
                    return ResultAjax.ok(204,"暂无数据");
                }
            }

        }
        return ResultAjax.ok(400,"参数不能为空");
    }

    @ApiOperation("获取所有门店信息接口")
    @PostMapping("/list")
    public ResultAjax list(@RequestBody StoreVo storeVo){

        log.info(storeVo.toString());
        PageHelper.startPage(storeVo.getPage(), storeVo.getLimit());
        List<Store> stores = storeService.storeList(storeVo);
        PageInfo<Store> pageInfo = new PageInfo<>(stores);
        return ResultAjax.ok(0,"ok",pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @ApiOperation("生成随机字符串门店ID接口")
    @PostMapping("/getId")
    public ResultAjax getId(@RequestBody @ApiParam("省市区VO类") ProvincesVo provincesVo){

        log.info(provincesVo.toString());
        //根据省市区获取随机门店id
        String storeId = GetStoreId.get(provincesVo.getProvince(), provincesVo.getCity(), provincesVo.getArea());
        return ResultAjax.ok(200,"ok",storeId);
    }

    @ApiOperation("新增门店接口")
    @PostMapping("/add")
    public ResultAjax add(@RequestBody @ApiParam(name = "门店实体类") Store store){

        log.info(store.toString());
        //保存门店信息
        storeService.save(store);
        //获取服务范围信息
        List<ServiceScope> serviceScopes = store.getServiceScopes();
        for (ServiceScope serviceScope : serviceScopes) {
            //获取保存的门店编号绑定到服务范围
            serviceScope.setStoreId(storeService.query()
                                .select("id")
                                .eq("store_name", store.getStoreName())
                                .one()
                                .getId());
        }
        //保存门店的服务范围信息
        serviceScopeService.saveBatch(serviceScopes);
        return ResultAjax.ok(200,"ok");
    }

    @ApiOperation("更新门店信息接口")
    @PutMapping("/update")
    public ResultAjax update(@RequestBody @ApiParam(name = "门店实体类") Store store){

        log.info(store.toString());
        //更新门店信息
        storeService.updateById(store);
        //获取服务范围信息
        List<ServiceScope> serviceScopes = store.getServiceScopes();
        //更新或添加服务范围信息：主键为空时新增;不为空则更新
        serviceScopeService.saveOrUpdateBatch(serviceScopes);
        if (!Objects.isNull(redisUtil.get("store:"+store.getId()))){
            redisUtil.set("store:"+store.getId(), storeService.getById(store.getId()),60*60*24*7);
            log.info("===更新redis===");
        }
        return ResultAjax.ok(200,"ok");
    }

    @ApiOperation("删除门店接口")
    @DeleteMapping("/delete")
    public ResultAjax delete(@RequestParam(value = "id",required = false) @ApiParam(value = "门店编号") Integer id){

        //删除门店
        storeService.removeById(id);
        //删除门店对应的服务范围信息
        QueryWrapper serviceScopeQueryWrapper = new QueryWrapper();
        serviceScopeQueryWrapper.eq("store_id",id);
        serviceScopeService.remove(serviceScopeQueryWrapper);
        redisUtil.del("store:"+id);
        log.info("===清除redis===");
        return ResultAjax.ok(200,"ok");
    }


}

